package com.zx.service.specification.impl;

import com.zx.dao.specification.SpecificationMapper;
import com.zx.dto.response.brand.BrandDto;
import com.zx.dto.response.specification.SpecificationBrandDto;
import com.zx.model.goods.SpecificationValue;
import com.zx.service.brand.impl.BrandServiceImpl;
import com.zx.service.specification.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunkai
 * @date 2022/8/5 0005 23:27
 * @mood zhou xun
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private SpecificationMapper specificationMapper;

    @Override
    public List<SpecificationBrandDto> querySpecificationValue(List<BrandDto> brandDtoList) {
        /*规格*/
        List<SpecificationBrandDto> specificationBrandDtoList = specificationMapper.querySpecification(brandDtoList);
            if (specificationBrandDtoList==null){
                return new ArrayList<>();
            }
        Map<Integer,SpecificationBrandDto> map=
        specificationBrandDtoList.stream().collect(Collectors.toMap(SpecificationBrandDto::getSpecificationId,a->a,(k1,k2)->k1));
        for (SpecificationBrandDto specificationBrandDto:specificationBrandDtoList){
                if (CollectionUtils.isEmpty(specificationBrandDto.getBrandDtoList())){
                    specificationBrandDto.setBrandDtoList(new ArrayList<>());
                }
                specificationBrandDto.getBrandDtoList().addAll(brandDtoList);
            }
        List<SpecificationBrandDto>  specificationBrandDtos=specificationMapper.querySpecificationValue(brandDtoList);
            //规格值
            for (SpecificationBrandDto specificationBrandDtoValue:specificationBrandDtos){
                SpecificationBrandDto specificationBrandDto = map.get(specificationBrandDtoValue.getSpecificationId());
                if (CollectionUtils.isEmpty(specificationBrandDto.getSpecificationValueDtoList())){
                    specificationBrandDto.setSpecificationValueDtoList(new ArrayList<>());
                }
                specificationBrandDto.getSpecificationValueDtoList().add(specificationBrandDtoValue);
            }
        return specificationBrandDtoList;
    }

    @Override
    public List<SpecificationBrandDto> querySpecification(List<BrandDto> brandDtoList) {
        return specificationMapper.querySpecification(brandDtoList);
    }
}
